// useAddAdminProduct.ts
import { AdminAddProductData } from "@/user.InterfaceLayer/Libraries/UI_KIT/Organisms/AdminAddModal.Organisms/type";
import { useMutation, useQueryClient } from "react-query";
import { adminBaseUrl } from "../..";
import { useMemo } from "react";

export const useAddAdminProduct = () => {
  const queryClient = useQueryClient();

  const addProductMutation = useMutation(
    async (newProduct: AdminAddProductData) => {
      const response = await fetch(adminBaseUrl, {
        method: "POST",
        headers: {
          "Content-Type": "application/json",
        },
        body: JSON.stringify(newProduct),
      });
      if (!response.ok) {
        throw new Error("Network response was not ok");
      }
      return response.json();
    },
    {
      onSuccess: () => {
        queryClient.invalidateQueries("products");
      },
    }
  );

  return useMemo(() => addProductMutation, [addProductMutation]);
};
